$(function(){
	//初始化地区
	var arr = area();
		for(var i=0; i<arr.length;i++){
			$("#change").append("<option value="+arr[i].areaCode+">"+arr[i].province+" "+arr[i].city+" "+arr[i].area+"</option>");
	}
	//初始化	
	initFileInput("headUrl", urlcore + "/api/attachment/upload","#oldheadUrl");
	initFileInput("identityBack", urlcore + "/api/attachment/upload","#oldidentityBack");
	initFileInput("licenseUrl", urlcore + "/api/attachment/upload","#oldlicenseUrl");
	initFileInput("identityFront", urlcore + "/api/attachment/upload","#oldidentityFront");
	initFileInput("storeUrl", urlcore + "/api/attachment/upload","#oldstoreUrl");

    var userId= getvl("userId");
        $('#thisId').text(userId);
	    $.ajax({
	        url: urlcore + "/api/user/account2Details?userId="+userId,
	        type: "get",
	        dataType: 'json',
	        contentType: "application/json;charset=utf-8",
	        success:function(data){
	            if (data.success == true) {           	
	                $('#realName').val(data.data.user.realName);
	                $('#id').val(data.data.user.secondId);
	                $('#sex').val(data.data.user.sex);
	                $('#phone').val(data.data.user.phone);
	                $('#idphone').val(data.data.user.phone);
	                $('#nickName').val(data.data.user.nickName);
	                $('#password').val(data.data.user.password);
	                $('#idCard').val(data.data.user.idCard);
	                $('#serveType').val(data.data.user.serveType);
	                $('#shopStartTime').val(data.data.user.shopStartTime);
	                $('#shopStartMoney').val(data.data.user.shopStartMoney);
	                $('#allMoney').val(data.data.allMoney);
	                $('#depositMoney').val(data.data.depositMoney);
	                var oldheadUrl = data.data.user.headUrl;
	                var oldidentityBack = data.data.user.identityBack;
	                var oldlicenseUrl = data.data.user.licenseUrl;
	                var oldidentityFront = data.data.user.identityFront;
	                var oldstoreUrl = data.data.user.storeUrl;
	                
	                if (oldheadUrl != null && oldheadUrl != '') {
	                    var imgarr = oldheadUrl.split("***");
	                    var imgStr = '';
	                    $.each(imgarr,function(index,value){
	                        imgStr += 
	                        	'<div style="position:relative;width:250px;height:250px;float:left;" display="block">'+
	                        	'	<span onclick="removeStrHtml(this)" style="position:absolute;top:10px;right:10px;font-size:20px;color:#f00;" class="glyphicon glyphicon-trash" aria-hidden="true">'+
	                        	'	</span>'+
	                        	'	<img style="padding: 5px 5px 5px 5px;" width="250px" height="250px" src="'+value+'">'+
	                    		'</div>';
	                    });
	                }
	                $('#oldheadUrl').html(imgStr);

	                if (oldlicenseUrl != null && oldlicenseUrl != '') {
	                    var imgarr = oldlicenseUrl.split("***");
	                    var imgStr2 = '';
	                    $.each(imgarr,function(index,value){
	                        imgStr2 += 
	                        	'<div style="position:relative;width:250px;height:250px;float:left;" display="block">'+
	                        	'	<span onclick="removeStrHtml(this)" style="position:absolute;top:10px;right:10px;font-size:20px;color:#f00;" class="glyphicon glyphicon-trash" aria-hidden="true">'+
	                        	'	</span>'+
	                        	'	<img style="padding: 5px 5px 5px 5px;" width="250px" height="250px" src="'+value+'">'+
	                    		'</div>';
	                    });
	                }
	                $('#oldlicenseUrl').html(imgStr2);

	                if (oldidentityBack != null && oldidentityBack != '') {
	                    var imgarr = oldidentityBack.split("***");
	                    var imgStr3 = '';
	                    $.each(imgarr,function(index,value){
	                        imgStr3 += 
	                        	'<div style="position:relative;width:250px;height:250px;float:left;" display="block">'+
	                        	'	<span onclick="removeStrHtml(this)" style="position:absolute;top:10px;right:10px;font-size:20px;color:#f00;" class="glyphicon glyphicon-trash" aria-hidden="true">'+
	                        	'	</span>'+
	                        	'	<img style="padding: 5px 5px 5px 5px;" width="250px" height="250px" src="'+value+'">'+
	                    		'</div>';
	                    });
	                }
	                $('#oldidentityBack').html(imgStr3);	
	                
	                if (oldidentityFront != null && oldidentityFront != '') {
	                    var imgarr = oldidentityFront.split("***");
	                    var imgStr4 = '';
	                    $.each(imgarr,function(index,value){
	                        imgStr4 += 
	                        	'<div style="position:relative;width:250px;height:250px;float:left;" display="block">'+
	                        	'	<span onclick="removeStrHtml(this)" style="position:absolute;top:10px;right:10px;font-size:20px;color:#f00;" class="glyphicon glyphicon-trash" aria-hidden="true">'+
	                        	'	</span>'+
	                        	'	<img style="padding: 5px 5px 5px 5px;" width="250px" height="250px" src="'+value+'">'+
	                    		'</div>';
	                    });
	                }
	                $('#oldidentityFront').html(imgStr4);
	                
	                if (oldstoreUrl != null && oldstoreUrl != '') {
	                    var imgarr = oldstoreUrl.split("***");
	                    var imgStr5 = '';
	                    $.each(imgarr,function(index,value){
	                        imgStr5 += 
	                        	'<div style="position:relative;width:250px;height:250px;float:left;" display="block">'+
	                        	'	<span onclick="removeStrHtml(this)" style="position:absolute;top:10px;right:10px;font-size:20px;color:#f00;" class="glyphicon glyphicon-trash" aria-hidden="true">'+
	                        	'	</span>'+
	                        	'	<img style="padding: 5px 5px 5px 5px;" width="250px" height="250px" src="'+value+'">'+
	                    		'</div>';
	                    });
	                }
	                $('#oldstoreUrl').html(imgStr5);	
	            } else if (data.code == 'OVERTIME'){
	                var thisUrl = window.location.href;
	                if (thisUrl.indexOf('login.html') <= -1) {
	                    top.window.location.href="login.html";
	                }
	            } else {
	                alert(data.msg);
	            }
	        },
	        error:function() {
	            /* Act on the event */
	            alert("error");
	        }
	    });
	    //初始化地图对象，加载地图
	   	var map = new AMap.Map("map", {resizeEnable: true});
		AMap.plugin(['AMap.ToolBar','AMap.Geocoder'],
	    function(){
	        map.addControl(new AMap.ToolBar());
	
	        map.addControl(new AMap.Geocoder());
	
		});
	    $.ajax({
            url: urlcore + "/api/address/addressByUserId?userId="+userId,
            type: "get",
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            success:function(data){
                if (data.success == true) {
                	var da = data.data;
                	$("#change option[value="+da.areaCode+"]").prop("selected", 'selected');
                	$('#addressDetail').val(da.addressDetail);
                	$('#lng').val(da.lng);
                	$('#lat').val(da.lat);
	          	var marker = new AMap.Marker({
		            position: [da.lng,da.lat],
		            map: map,
        			raiseOnDrag: true
		        });
		        map.setFitView();
			    map.on('click', function(e) {
			    	var lng =e.lnglat.getLng();
			    	var lat=e.lnglat.getLat();
			        marker.setPosition([lng,lat]);
			        $('#lng').val(lng);
                	$('#lat').val(lat);
			    });
                } else if (data.code == 'OVERTIME'){
	                    var thisUrl = window.location.href;
	                    if (thisUrl.indexOf('login.html') <= -1) {
	                        top.window.location.href="login.html";
	                    }
	                } else {
	                    alert(data.msg);
	                }
            },
            error:function() {
                /* Act on the event */
                alert("error");
            }
        });
});


//添加
function release(){
	var userId=$('#thisId').text()
	var realName = $('#realName').val();
	var id = $('#id').val();
	var sex = $('#sex').val();
	var phone = $('#phone').val();
	var password = $('#password').val();
	var idCard = $('#idCard').val();
	var serveType = $('#serveType').val();
	var nickName = $('#nickName').val();
	var shopStartTime = $('#shopStartTime').val();
	var shopStartMoney = $('#shopStartMoney').val();
	var allMoney = $('#allMoney').val();
	var depositMoney = $('#depositMoney').val();
	
	//地区
	var addressDetail = $('#addressDetail').val();
	var content = $("#change").find("option:selected").text();
	var arr = content.split(' ');
	var provinceName = arr[0];
	var cityName = arr[1];
	var areaName = arr[2];
	var areaCode = $("#change").val();
	var lng = $("#lng").val();
	var lat = $("#lat").val();
	
    //图片地址
    var headUrl = "";
    $("#oldheadUrl img").each(function(index,item){
		headUrl = $(this).attr("src")+"***";
	});
    headUrl = headUrl.substring(0, headUrl.length-3);
    
    var licenseUrl = "";
    $("#oldlicenseUrl img").each(function(index,item){
		licenseUrl = $(this).attr("src")+"***";
	});
    licenseUrl = licenseUrl.substring(0, licenseUrl.length-3);
    
    var identityBack = "";
    $("#oldidentityBack img").each(function(index,item){
		identityBack = $(this).attr("src")+"***";
	});
    identityBack = identityBack.substring(0, identityBack.length-3);
    
    var identityFront = "";
    $("#oldidentityFront img").each(function(index,item){
		identityFront = $(this).attr("src")+"***";
	});
    identityFront = identityFront.substring(0, identityFront.length-3);
    
    var storeUrl = "";
    $("#oldstoreUrl img").each(function(index,item){
		storeUrl = $(this).attr("src")+"***";
	});
    storeUrl = storeUrl.substring(0, storeUrl.length-3);

    $.ajax({
        url:  urlcore + "/api/user/updateUser?userId="+userId,
        type: "post",
       	data:JSON.stringify({
	        "headUrl": headUrl,
	        "licenseUrl":licenseUrl,
	        "identityBack": identityBack,
	        "identityFront": identityFront,
	        "storeUrl": storeUrl,
	        "sex": sex,	
	        "realName":realName,
	        "secondId": id,
	        "phone": phone,
	        "password": password,
	        "idCard":idCard,
	        "serveType": serveType,
	        "nickName": nickName,
	        "shopStartTime": shopStartTime,
	        "shopStartMoney": shopStartMoney
		}),
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success:function(data){
            if (data.success == true) {
                    alert("修改成功");
                    //跳转列表页
                    window.location.href = "shop_update.html?userId="+userId;               
            } else if (data.code == 'OVERTIME') {
                var thisUrl = window.location.href;
                if (thisUrl.indexOf('login.html') <= -1) {
                    top.window.location.href = "login.html";
                }
            } else {
                alert(data.msg);
            }

        },
        error: function() {
            alert("error");
        }

    });
    
    //添加地区
    $.ajax({
        url:  urlcore + "/api/address/updateUserAddress?userId="+userId,
        type: "post",
       	data:JSON.stringify({
	        "addressDetail": addressDetail,
	        "province":provinceName,
	        "city": cityName,
	        "county": areaName,
	        "areaCode": areaCode,
	        "lng": lng,
	        "lat": lat
		}),
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success:function(data){
        	if (data.success == true) {
            } else if (data.code == 'OVERTIME') {
                var thisUrl = window.location.href;
                if (thisUrl.indexOf('login.html') <= -1) {
                    top.window.location.href = "login.html";
                }
            } else {
                alert(data.msg);
            }

        },
        error: function() {
            alert("error");
        }

    });
}

//获取所属区域
function area(){
	var da='';
	$.ajax({
            url: urlcore + "/api/openArea/allArea",
            type: "get",
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            async: false,
            success:function(data){
                if (data.success == true) {
                    da = data.data;                   
                } else if (data.code == 'OVERTIME'){
                    var thisUrl = window.location.href;
                    if (thisUrl.indexOf('login.html') <= -1) {
                        top.window.location.href="login.html";
                    }
                } else {
                    alert(data.msg);
                }
            },
            error:function() {
                /* Act on the event */
                alert("error");
            }
        });
        return da;
}